package com.hqyj.springBoot.modules.test.controller;

import com.hqyj.springBoot.modules.common.entity.ResultEntity;
import com.hqyj.springBoot.modules.common.entity.SearchBean;
import com.hqyj.springBoot.modules.test.entity.Student;
import com.hqyj.springBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

//代表该类的方法都是数据没有页面的渲染
@RestController //相当于@RequestBody + @Controller
@RequestMapping(value = "/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student ---- post
     * {"studentName":"baiyun"}
     * {"studentName":"baiyun", "studentCard":{"cardNo":"student_cart_00001"}}
     */
    @PostMapping(value = "/student",consumes = "application/json")
    public ResultEntity<Student> insertStudent(@RequestBody Student student){
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student
     * {"id":9, "studentName":"BAIYUN"}
     */
    @PutMapping(value = "/student",consumes = "application/json")
    public ResultEntity<Student> updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/2 ---- delete
     */
    @DeleteMapping("/student/{studentId}")
    public ResultEntity<Object> deleteStudent(@PathVariable int studentId) {
        return studentService.deleteStudentById(studentId);
    }

    /**
     * 127.0.0.1/api/student/8 ---- get
     */
    @GetMapping("/student/{studentId}")
    public Student selectStudentByStudentId(@PathVariable int studentId){
        return studentService.selectStudentByStudentId(studentId);
    }

    /**
     * 127.0.0.1/api/students ---- post
     * {"currentPage":1, "pageSize":5, "keyWord":"bai", "orderBy":"studentName",
     * "direction":"asc"}
     */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean){
        return studentService.getStudentsBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1/api/student?studentName=baiyun1 ---- get
     */
    @GetMapping("/student")
    public Student getStudentByName(@RequestParam String studentName) {
        return studentService.getStudentByName(studentName);
    }
}
